
var autodb = require('../../db/auto.db')
var logger = require('../../logger')

function onsocket(socket, data) {
    if (data.maintag == null) {
        return logger.log('data.maintag is null');
    }
    if (data.child == null) {
        return logger.log('data.child is null');
    }
    if (data.grandson == null) {
        return logger.log('data.grandson is null');
    }
    let table = data.maintag.toLowerCase() + '.' + data.childtag.toLowerCase() + config.grandsontags;
    autodb.client(data.isMusic).sadd(table, data.grandson.toLowerCase(), (err, response) => {
        if (err) {
            logger.log("error", "sadd", data.grandson, err);
        } else {
            autodb.gettagtree(data.isMusic, (err, tree) => {
                if (err) {
                    emit.send(socket, "gettaglist", null);
                } else {
                    emit.send(socket, "gettaglist", tree);
                }
            });
        }
    })
}

module.exports = {
    onsocket
}